<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of PlayerMatchReadiness
 *
 * @author Laszlo.Pokoradi
 */
class PlayerMatchReadiness {
  const LEAGUECURRENT_XPATH = "./league[1]/@current";
  const LEAGUEEXHAUSTED_XPATH = "./league[1]/@exhausted";
  const LEAGUEINJURYSTATUS_XPATH = "./league[1]/injury[1]/@status";
  const LEAGUESUSPENSIONSTATUS_XPATH = "./league[1]/suspension[1]/@status";
  const CUPCURRENT_XPATH = "./cup[1]/@current";
  const CUPEXHAUSTED_XPATH = "./cup[1]/@exhausted";
  const CUPINJURYSTATUS_XPATH = "./cup[1]/injury[1]/@status";
  const CUPSUSPENSIONSTATUS_XPATH = "./cup[1]/suspension[1]/@status";

  private $leagueCurrent;
  private $leagueExhausted;
  private $leagueInjuryStatus;
  private $leagueSuspensionStatus;
  private $cupCurrent;
  private $cupExhausted;
  private $cupInjuryStatus;
  private $cupSuspensionStatus;
  private $db;

  public function __construct($database) {
    $this->db = $database;

    $this->leagueCurrent = false;
    $this->leagueExhausted = false;
    $this->leagueInjuryStatus = false;
    $this->leagueSuspensionStatus = false;
    $this->cupCurrent = false;
    $this->cupExhausted = false;
    $this->cupInjuryStatus = false;
    $this->cupSuspensionStatus = false;
  }

  public function loadFromNode(DOMElement $docNode) {
    $doc = $docNode->ownerDocument;

    $xPath = new DOMXPath($doc);

    $qResult = $xPath->query(PlayerMatchReadiness::LEAGUECURRENT_XPATH,$docNode);
    $this->leagueCurrent = $qResult->item(0)->nodeValue;
    $qResult = $xPath->query(PlayerMatchReadiness::LEAGUEEXHAUSTED_XPATH,$docNode);
    $this->leagueExhausted = $qResult->item(0)->nodeValue;
    $qResult = $xPath->query(PlayerMatchReadiness::LEAGUEINJURYSTATUS_XPATH,$docNode);
    $this->leagueInjuryStatus = $qResult->item(0)->nodeValue;
    $qResult = $xPath->query(PlayerMatchReadiness::LEAGUESUSPENSIONSTATUS_XPATH,$docNode);
    $this->leagueSuspensionStatus = $qResult->item(0)->nodeValue;

    $qResult = $xPath->query(PlayerMatchReadiness::CUPCURRENT_XPATH,$docNode);
    $this->cupCurrent = $qResult->item(0)->nodeValue;
    $qResult = $xPath->query(PlayerMatchReadiness::CUPEXHAUSTED_XPATH,$docNode);
    $this->cupExhausted = $qResult->item(0)->nodeValue;
    $qResult = $xPath->query(PlayerMatchReadiness::CUPINJURYSTATUS_XPATH,$docNode);
    $this->cupInjuryStatus = $qResult->item(0)->nodeValue;
    $qResult = $xPath->query(PlayerMatchReadiness::CUPSUSPENSIONSTATUS_XPATH,$docNode);
    $this->cupSuspensionStatus = $qResult->item(0)->nodeValue;
  }

  public function insertNewToDB($playerId) {
    $this->db->set('playerId',$playerId);
    $this->db->set('leagueCurrent',$this->leagueCurrent);
    $this->db->set('leagueExhausted',$this->leagueExhausted);
    $this->db->set('leagueInjuryStatus',$this->leagueInjuryStatus);
    $this->db->set('leagueSuspensionStatus',$this->leagueSuspensionStatus);
    $this->db->set('cupCurrent',$this->cupCurrent);
    $this->db->set('cupExhausted',$this->cupExhausted);
    $this->db->set('cupInjuryStatus',$this->cupInjuryStatus);
    $this->db->set('cupSuspensionStatus',$this->cupSuspensionStatus);
    $query = $this->db->insert('playermatchreadiness');

    if (!$query) {
      log_message('error', 'Error during playermatchreadiness inserttion!');
      return false;
    }

    return $this->db->insert_id();
  }

  public function amendOldInDB($id) {
    $this->db->where('id',$id);
    $query = $this->db->get('playermatchreadiness');

    if (!$query) {
      log_message('error','Error during playermatchreadiness amendment!');
    } else {
      $tRow = $query->row();
      $oldId = $tRow->id;
      $playerId = $tRow->playerId;

      if ($tRow->leagueCurrent!=$this->leagueCurrent
          || $tRow->leagueExhausted!=$this->leagueExhausted
          || $tRow->leagueInjuryStatus!=$this->leagueInjuryStatus
          || $tRow->leagueSuspensionStatus!=$this->leagueSuspensionStatus
          || $tRow->cupCurrent!=$this->cupCurrent
          || $tRow->cupExhausted!=$this->cupExhausted
          || $tRow->cupInjuryStatus!=$this->cupInjuryStatus
          || $tRow->cupSuspensionStatus!=$this->cupSuspensionStatus) {
        $newId = $this->insertNewToDB($playerId);

        $this->db->set('playerId', $playerId);
        $this->db->set('table','playermatchreadiness');
        $this->db->set('oldId',$oldId);
        $this->db->set('newId',$newId);
        $this->db->set('insertTime',date('Y-m-d H:i:s'));
        $query = $this->db->insert('playerhistory');

        if (!$query) {
          log_message('error','Playermatchreadiness amend was unsuccesful!');
        } else {
          return $newId;
        }
      }
    }
    return $oldId;
  }

  public function loadFromDB($matchReadinessId) {
    $this->db->where('id',$matchReadinessId);
    $query = $this->db->get('playermatchreadiness');

    if (!$query || $query->num_rows()==0) {
      log_message('error', 'PlayerMatchReadiness ('.$matchReadinessId.') not found in the database!');
    } else {
      $result = $query->row();

      $this->leagueCurrent = $result->leagueCurrent;
      $this->leagueExhausted = $result->leagueExhausted;
      $this->leagueInjuryStatus = $result->leagueInjuryStatus;
      $this->leagueSuspensionStatus = $result->leagueSuspensionStatus;
      $this->cupCurrent = $result->cupCurrent;
      $this->cupExhausted = $result->cupExhausted;
      $this->cupInjuryStatus = $result->cupInjuryStatus;
      $this->cupSuspensionStatus = $result->cupSuspensionStatus;
    }
  }
}
?>
